<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	public $cacheId = '';

	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/main';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();

    public $selectCategories = array();

    public $oPager = null;

	protected $_userInfo = array();

	protected $_title = array();
	protected $_keywords = array();
	protected $_description = '';
	protected $_aMetaOg = array();

    public function __construct($id, $module = null)
    {
        parent::__construct($id, $module);

        $this->oPager = new CPagination(100);
        $this->oPager->setPageSize(Yii::app()->params['listPerPage']);

        $curPage = abs(Yii::app()->getRequest()->getParam('page', 0));
//        $curPage =
        $this->oPager->setCurrentPage($curPage > 0 ? $curPage-1 : 0);

	    $this->cacheId = Yii::app()->getRequest()->requestUri;

//	    $l = new GetText();
//	    $l->language = Yii::app()->params->locales[Yii::app()->language];
//	    $l->locale_dir = LOCALE_DIR;
//	    $l->init();

//	    $this->_lang = Yii::app()->language;
//	    $this->currentLang = $this->_lang;

        $oSession = new CHttpSession();
        $oSession->open();
	    if(Yii::app()->user && !empty($oSession['userInfo']))
		    $this->_userInfo = $oSession['userInfo'];
    }

	public function init()
	{
		parent::init();
	}

	public function filters()
	{
		return array(
			'accessControl',
//			array(
//				'CHttpCacheFilter + index',
//				'lastModified'=>time() + 600,
//			),
//			array(
//				'COutputCache',
//				'duration' => 300,
////				'requestType' => array('GET'),
//				'varyByRoute' => true,
//				'varyByParam' => array('id', 'v', 'page'),
//			),
//			array(
//				'CHttpCacheFilter + index',
//				'lastModified'=>Yii::app()->db->createCommand("SELECT MAX(`date_add`) FROM {{goods}}")->queryScalar(),
//			),
		);
	}

	public function accessRules()
	{
		$controller = 'controller';

		return array(
//			array('deny',
//			      'actions'=>array('create', 'delete', 'update', 'edit', 'editor', 'add'),
//			      'users'=>array('?'),
//			),
//			array('allow',
//			      'actions'=>array('create', 'delete', 'update', 'edit', 'editor', 'add'),
//			      'roles'=>array('admin'),
//			),
//			array('allow',
//			      'actions'=>array('add'),
//			      'roles'=>array('guest'),
//			      'expression' => function($user, $rule) {
//				      return isset(Yii::app()->controller)
//				            && Yii::app()->controller->id == 'cart'
//				            && Yii::app()->controller->module->id == 'goods';
//			      }
//			),
//			array('deny',
//			      'actions'=>array('create', 'delete', 'update', 'edit', 'editor', 'add'),
//			      'users'=>array('*'),
//			),
		);
	}

    protected function _isXmlHttpRequest()
    {
        return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
    }

    protected function _getParam($name, $default = null)
    {
        if(isset($_GET[$name]))
            return $_GET[$name];

        if(isset($_POST[$name]))
            return $_POST[$name];

        return $default;
    }

    protected function _getParams()
    {
        return $_REQUEST;
    }

    protected function _getPost()
    {
        return $_POST;
    }

    protected function _isPost()
    {
        return count($_POST) > 0;
    }

	public function addTitle($title)
	{
		$this->_title[$title] = $title;

		return $this;
	}

	public function getTitle()
	{
		$this->_title[] =  'Интернет магазин '. Yii::app()->name;
		$title = implode(' | ', $this->_title);

		return $title;
	}

	public function addKeywords($keyword)
	{
		$this->_keywords[$keyword] = trim($keyword);

		return $this;
	}

	public function getKeywords()
	{
		$keywords = implode(' ', $this->_keywords);// .' '. Yii::app()->name;
		$keywords = preg_replace('`\s{2,}|\,+|\.+|\-+|\_+|\"+|\'+`i', ' ', $keywords);
		$keywords = preg_replace('`\s{2,}|\,+|\.+|\-+|\_+|\"+|\'+`i', ' ', $keywords);
		$aKeywords = explode(' ', $keywords);

		$keywords = '';
		foreach($aKeywords as $keyword)
		{
			if($keywords != '')
				$keywords .= ', ';

			$keywords .= trim($keyword);
		}

		return $keywords;
	}

	public function addDescription($description)
	{
		$this->_description .= $description;

		return $this;
	}

	public function getDescription()
	{
		return $this->_description;
	}

	public function setOg(array $og)
	{
		$this->_aMetaOg = array(
			'type' => 'product',
			'title' => 'futbolata dc',
			'url' => 'http://'.Yii::app()->params['host']. $_SERVER['REQUEST_URI'],
			'image' => '',
			'description' => Yii::app()->name,
			'site_name' => Yii::app()->name,
		);

		$this->_aMetaOg['title'] = $og['title'];
		if(!empty($og['image']))
			$this->_aMetaOg['image'] = 'http://'. Yii::app()->params['host']. $og['image'];

		$this->_aMetaOg['description'] = $og['description'];


	}

	public function getMetaOg()
	{
		return $this->_aMetaOg;
	}

}